<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="国籍" prop="nationality">
        <el-input
          v-model="queryParams.nationality"
          placeholder="请输入国籍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contcatWay">
        <el-input
          v-model="queryParams.contcatWay"
          placeholder="请输入联系方式"
          maxlength="11"
          show-word-limit
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身体状况" prop="physicalCondition">
        <el-select v-model="queryParams.physicalCondition" clearable>
          <el-option
            v-for="item in physicalConditionList1"
            :label="item.label"
            :value="item.value"
            :key="item.value"
            >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="核酸检测结果" prop="nucieicAcidTestResult">
        <el-select v-model="queryParams.nucieicAcidTestResult" placeholder="请选择核酸检测结果" clearable>
          <el-option
            v-for="item in nucieicAcidTestResultList1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="队伍联系方式" prop="teamContact">
        <el-input
          v-model="queryParams.teamContact"
          placeholder="请输入队伍联系方式"
          maxlength="11"
          show-word-limit
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领队名称" prop="leaderName">
        <el-input
          v-model="queryParams.leaderName"
          placeholder="请输入领队名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small " style="margin-left: 30px" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small "
          @click="handleAdd"
          v-hasPermi="['system:athlete:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="small "-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:athlete:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:athlete:remove']"
        >批量删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="small"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:athlete:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table  border v-loading="loading" :data="athleteList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"  align="center" />
      <el-table-column label="id" align="center" v-if="columns[0].visible" prop="id" />
      <el-table-column label="姓名" align="center" v-if="columns[1].visible" prop="name" />
      <el-table-column label="性别" align="center" v-if="columns[2].visible" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" v-if="columns[3].visible" prop="age" />
      <el-table-column label="个人证件照" align="center" v-if="columns[12].visible" prop="imageName" width="150px" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageName" :width="100" :height="100"/>
        </template>
      </el-table-column>
      <el-table-column label="国籍" align="center" v-if="columns[4].visible" prop="nationality" />
      <el-table-column label="联系方式" align="center" v-if="columns[5].visible" prop="contcatWay" width="180px"/>
      <el-table-column label="邮箱" align="center" v-if="columns[6].visible" prop="mailbox" width="180px" />
      <el-table-column label="参与赛事" align="center" v-if="columns[7].visible" prop="participateEvent" width="180px">
        <template slot-scope="scope">
          {{scope.row.participateEvent == '0' ? '2022年北京冬奥会':'2022年北京残奥会'}}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columns[8].visible"
        label="身体状况" align="center" prop="physicalCondition"
        :filters="[{ text: '正常', value: '1' }, { text: '良好', value: '2' },{text:'异常',value:'3'}]"
        :filter-method="filterTag1"
      width="120px">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.physicalCondition === '1' ? 'primary' : scope.row.physicalCondition == '2' ? 'success':'danger'"
              disable-transitions>{{scope.row.physicalCondition == '1' ? '正常':scope.row.physicalCondition == '2' ? '良好':'异常' }}</el-tag>
          </template>
      </el-table-column>
      <el-table-column
        label="核酸检测结果"
        align="center"
        v-if="columns[9].visible"
        prop="nucieicAcidTestResult" width="180px"
        :filters="[{ text: '阴性', value: '1' }, { text: '阳性', value: '0' }]"
        :filter-method="filterTag">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.nucieicAcidTestResult === '1' ? 'success' : 'danger'"
            disable-transitions>{{scope.row.nucieicAcidTestResult === '1' ? '阴性' :'阳性'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="队伍联系方式" align="center" v-if="columns[10].visible" prop="teamContact" width="180px" />
      <el-table-column label="领队名称" align="center" v-if="columns[11].visible" prop="leaderName" width="180px"/>
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:athlete:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:athlete:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参赛选手信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="姓名" prop="name">
          <el-input maxlength="20" show-word-limit v-model="form.name" placeholder="请输入姓名"  style="width: 240px"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" style="width: 240px">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age" >
          <el-input v-model="form.age" placeholder="请输入年龄" style="width: 240px" />
        </el-form-item>
        <el-form-item label="个人证件照" prop="imageName">
          <image-upload v-model="form.imageName"/>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入国籍" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contcatWay">
          <el-input v-model="form.contcatWay" placeholder="请输入联系方式"  maxlength="11" show-word-limit style="width: 240px"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="mailbox">
          <el-input v-model="form.mailbox" placeholder="请输入邮箱" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="参与赛事" prop="participateEvent">
          <el-select v-model="form.participateEvent" placeholder="请选择参与赛事">
            <el-option
              v-for="item in participateEventList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身体状况" prop="physicalCondition" >
          <el-select v-model="form.physicalCondition" clearable style="width: 240px">
            <el-option
            v-for="item in physicalConditionList"
            :label="item.label"
            :value="item.value"
            :key="item.value"
            clearable>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="核酸检测结果" prop="nucieicAcidTestResult" >
          <el-select v-model="form.nucieicAcidTestResult" placeholder="请选择核酸检测结果" clearable style="width: 240px">
            <el-option
              v-for="item in nucieicAcidTestResultList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="队伍联系方式" prop="teamContact" >
          <el-input v-model="form.teamContact" placeholder="请输入队伍联系方式" maxlength="11" show-word-limit style="width: 240px"/>
        </el-form-item>
        <el-form-item label="领队名称" prop="leaderName">
          <el-input v-model="form.leaderName" placeholder="请输入领队名称" maxlength="20" show-word-limit style="width: 240px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAthlete, getAthlete, delAthlete, addAthlete, updateAthlete } from "@/api/system/athlete";

export default {
  name: "Athlete",
  dicts: ['sys_user_sex', 'sys_job_group'],
  data() {
    return {
      //查询条件
      physicalConditionList1:[
        {value:'1',label:'正常'},
        {value:'2',label:'良好'},
        {value:'3',label:'异常'}
      ],
      // 弹窗
      physicalConditionList:[
        {value:'1',label:'正常'},
        {value:'2',label:'良好'},
        {value:'3',label:'异常'}
      ],
      participateEventList:[
        {value:'0',label:'2022年北京冬奥会'},
        {value:'1',label:'2022年北京残奥会'}
      ],
      //查询条件
      nucieicAcidTestResultList1:[
        {value: '1',
          label: '阴性'
        },
        {
          value:'0',
          label:'阳性'
        }
      ],
      //弹窗
      nucieicAcidTestResultList:[
        {value: '1',
        label: '阴性'
        },
        {
          value:'0',
          label:'阳性'
        }
      ],
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 列信息
      columns: [
        { key: 0, label: `id`, visible: true },
        { key: 1, label: `姓名`, visible: true },
        { key: 2, label: `性别`, visible: true },
        { key: 3, label: `年龄`, visible: true },
        { key: 4, label: `国籍`, visible: true },
        { key: 5, label: `联系方式`, visible: true },
        { key: 6, label: `邮箱`, visible: true },
        { key: 7, label: `参与赛事`, visible: true },
        { key: 8, label: `身体状况`, visible: true },
        { key: 9, label: `核酸检测检测结果`, visible: true },
        { key: 10, label: `队伍联系方式`, visible: true },
        { key: 11, label: `领队名称`, visible: true },
        { key: 12, label: `个人证件照`, visible: true }
      ],
      // 总条数
      total: 0,
      // 参赛选手信息表格数据
      athleteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        nationality: null,
        contcatWay: null,
        physicalCondition: null,
        nucieicAcidTestResult: null,
        teamContact: null,
        leaderName: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        dialogImageUrl: [
          { required: true, message: "证件照不能为空", trigger: "blur" }
        ],
        nationality: [
          { required: true, message: "国籍不能为空", trigger: "blur" }
        ],
        imageName: [
          { required: true, message: "个人证件照不能为空", trigger: "blur" }
        ],
        contcatWay: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        mailbox: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        participateEvent: [
          { required: true, message: "参与赛事不能为空", trigger: "change" }
        ],
        physicalCondition: [
          { required: true, message: "身体状况不能为空", trigger: "blur" }
        ],
        nucieicAcidTestResult: [
          { required: true, message: "核酸检测结果不能为空", trigger: "blur" }
        ],
        teamContact: [
          { required: true, message: "队伍联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        leaderName: [
          { required: true, message: "领队名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    filterTag(value,row){
      return row.nucieicAcidTestResult === value;
    },
    filterTag1(value,row){
      return row.physicalCondition === value;
    },
    handleDownload(file) {
      window.open(file.url)
    },
    // 3、删除--根据 url 的不同进行匹配删除
    handleRemove(file) {
      let uploadFiles = this.$refs.pictureUpload.uploadFiles
      for (var i = 0; i < uploadFiles.length; i++) {
        if (uploadFiles[i]['url'] == file.url) {
          uploadFiles.splice(i, 1)
        }
      }
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePictureCardPreview(file) {
      debugger
      this.form.dialogImageUrl = file.url
      this.dialogVisible = true
    },

    /** 查询参赛选手信息列表 */
    getList() {
      this.loading = true;
      listAthlete(this.queryParams).then(response => {
        this.athleteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        age: null,
        dialogImageUrl:'',
        nationality: null,
        contcatWay: null,
        mailbox: null,
        participateEvent: null,
        physicalCondition: null,
        nucieicAcidTestResult: null,
        teamContact: null,
        leaderName: null,
        extField: null,
        extField1: null,
        extField2: null,
        isDeleted: null,
        createUserid: null,
        createUsername: null,
        createTime: null,
        serverIp: null,
        updateUserid: null,
        updateUsername: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参赛选手信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAthlete(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参赛选手信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAthlete(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAthlete(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除参赛选手信息编号为"' + ids + '"的数据项？').then(function() {
        return delAthlete(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/athlete/export', {
        ...this.queryParams
      }, `athlete_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.c-red{
  color:#ff4949;
}
.c-green{
  color: #13ce66;
}
.c-blue{
  color:blue;
}
</style>
